/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: CategoryServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2022/12/29 20:48
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.bo.CategoryBo;
import com.cskaoyan.bean.bo.CategoryFaBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.CategoryLiVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/*
* 用于商品的回显
* */
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Override
    public CommonData getAllCategoryInfo() {
        //开启分页
        //根据pid的值为0 查询出所有的一级标题的信息
        //新建example
        int count=0;
        MarketCategoryExample faExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = faExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(0);
        //查出所有的一级标题内容
        List<MarketCategory> ParentCategories = marketCategoryMapper.selectByExample(faExample);
        //查出所有的二级内容
        MarketCategoryExample sonExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria sonCriteria = sonExample.createCriteria();
        sonCriteria.andDeletedEqualTo(false);
        sonCriteria.andPidNotEqualTo(0);
        //sql查询语句 所有二级标题的集合
        LinkedList<CategoryFaBo> categoryFaBos = new LinkedList<>();
        List<MarketCategory> sonCategory = marketCategoryMapper.selectByExample(sonExample);
        for (MarketCategory parentCategory : ParentCategories) {
            //System.out.println("进来了吗");
            CategoryFaBo categoryFaBo = new CategoryFaBo();
            categoryFaBo.setDesc(parentCategory.getDesc());
            categoryFaBo.setIconUrl(parentCategory.getIconUrl());
            categoryFaBo.setId(parentCategory.getId());
            categoryFaBo.setLevel(parentCategory.getLevel());
            categoryFaBo.setName(parentCategory.getName());
            categoryFaBo.setPicUrl(parentCategory.getPicUrl());
            categoryFaBo.setKeywords(parentCategory.getKeywords());
            //预留空集合
            LinkedList<CategoryBo> sonList=new LinkedList<>();
            for (MarketCategory marketCategory : sonCategory) {
                //查询当前一级标题下的所有二级标题
                //下面这个得到当前标题下的二级标题的集合
                //System.out.println(parentCategory.getId());
                //System.out.println(marketCategory.getPid());
                if(parentCategory.getId().equals(marketCategory.getPid())){
                    //System.out.println("到这里了吗");
                    CategoryBo categoryBo = new CategoryBo();
                    categoryBo.setDesc(marketCategory.getDesc());
                    categoryBo.setIconUrl(marketCategory.getIconUrl());
                    categoryBo.setId(marketCategory.getId());
                    categoryBo.setLevel(marketCategory.getLevel());
                    categoryBo.setName(marketCategory.getName());
                    categoryBo.setPicUrl(marketCategory.getPicUrl());
                    categoryBo.setKeywords(marketCategory.getKeywords());
                    //System.out.println(categoryBo);
                    //将当前的属于一级标题下的内容假如集合中去
                    sonList.add(categoryBo);
                   // System.out.println(categoryBo);
                }
                //System.out.println(sonList);
                categoryFaBo.setChildren(sonList);
               // System.out.println(categoryFaBo);
            }
            //将一个个父类加入集合中
            categoryFaBos.add(categoryFaBo);
        }

        System.out.println(categoryFaBos);
      /*  PageInfo<MarketCategory> PageInfo = new PageInfo<>();
        int total = PageInfo.getPageNum();*/
        //再根据当前的一级标题查询出所有的二级标题的内容 且放入对象中
        //放入commondata中
        CommonData<CategoryFaBo> categoryFaBoCommonData = new CommonData<>();
        count=ParentCategories.size()+sonCategory.size();
        categoryFaBoCommonData.setList(categoryFaBos);
        categoryFaBoCommonData.setTotal(count);
        categoryFaBoCommonData.setLimit(count);
        categoryFaBoCommonData.setPage(1);
        categoryFaBoCommonData.setPages(1);
        System.out.println(categoryFaBoCommonData);
        return categoryFaBoCommonData;
    }
/*
* 添加操作
*
* */
    @Override
    public MarketCategory addCategoryInfo(MarketCategory categoryBo) {
        //获得添加的时间以及修改的时间
        Date currentDate = new Date(System.currentTimeMillis());
        //将当前的时间赋值给需要插入的对象
        categoryBo.setAddTime(currentDate);
        categoryBo.setUpdateTime(currentDate);
        marketCategoryMapper.insertSelective(categoryBo);
        System.out.println(categoryBo);
        return categoryBo;
    }
/*
* 删除类目表 状态码0====>1
* */
    @Override
    public void deleteCategoryByClick(MarketCategory category) {
        //创建example对象
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(category.getId());
        marketCategory.setDeleted(true);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }
/*
* 更新类目表
*
* */
    @Override
    public void updateCategory(MarketCategory category) {
        //把修改时间填入
        Date updateTime = new Date(System.currentTimeMillis());
        category.setUpdateTime(updateTime);
        marketCategoryMapper.updateByPrimaryKeySelective(category);

    }
/*
* li回显  查询出所有的l1的标题且回显
* */
    @Override
    public CommonData<CategoryLiVo> getL1InfoShow() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        //查询出所有的为l1的表
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(marketCategoryExample);
        LinkedList<CategoryLiVo> categoryLiVos = new LinkedList<>();
        for (MarketCategory marketCategory : categoryList) {
            CategoryLiVo categoryLiVo = new CategoryLiVo();
            //需要的是name加level
            categoryLiVo.setLabel(marketCategory.getName()+marketCategory.getLevel());
            categoryLiVo.setValue(marketCategory.getId());
            categoryLiVos.add(categoryLiVo);
        }
        //获取分页信息
        PageInfo<CategoryLiVo> categoryLiVoPageInfo = new PageInfo<>(categoryLiVos);
        int pages = categoryLiVoPageInfo.getPages();
        int pageSize = categoryLiVoPageInfo.getPageSize();
        int pageNum = categoryLiVoPageInfo.getPageNum();
        //给commonData对象赋值
        CommonData<CategoryLiVo> categoryLiVoCommonData = new CommonData<>();
        categoryLiVoCommonData.setList(categoryLiVos);
        categoryLiVoCommonData.setLimit(pageSize);
        categoryLiVoCommonData.setTotal(pageSize);
        categoryLiVoCommonData.setPage(pageNum);
        categoryLiVoCommonData.setPages(pages);
        return categoryLiVoCommonData;
    }
}
